package pl.edu.agh.iosr.gamblingzone.model;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

/**
 * The Class Group.
 */
@AttributeOverride(name="id",
		column=@Column(name="GROUP_ID"))
@Entity(name="GROUPS")
@Inheritance(strategy = InheritanceType.JOINED)
public class Group extends EntityModel {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 7856341797897118230L;

	/** The name. */
	@Column(name="GROUPS_NAME", nullable=false)
	private String name;
	
	/** The description. */
	@Column(name="GROUPS_DESCRIPTION", nullable=false)
	private String description;
	
	/** The discipline. */
	@ManyToOne(targetEntity=Discipline.class, 
			cascade={CascadeType.PERSIST, CascadeType.MERGE})
	@JoinColumn(name = "DISCIPLINE_ID", nullable=false)
	private Discipline discipline;
	
	/** The events. */
	@OneToMany(mappedBy="group", cascade=CascadeType.ALL)
	private List<AbstractEvent> events = new LinkedList<AbstractEvent>();
	
	/**
	 * Gets the discipline.
	 * 
	 * @return the discipline
	 */
	public Discipline getDiscipline() {
		return discipline;
	}

	/**
	 * Sets the discipline.
	 * 
	 * @param discipline the new discipline
	 */
	protected void setDiscipline(Discipline discipline) {
		this.discipline = discipline;
	}

	/**
	 * Gets the name.
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name.
	 * 
	 * @param name the new name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Gets the description.
	 * 
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Sets the description.
	 * 
	 * @param description the new description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Gets the events.
	 * 
	 * @return the events
	 */
	public List<AbstractEvent> getEvents() {
		return events;
	}
	
	/**
	 * Adds the event.
	 * 
	 * Automatically creates bidirectional connection between this Group and given AbstractEvent.
	 * Due to cascade mechanism given AbstractEvent is made persistent.
	 * 
	 * @param ae the ae
	 */
	public void addEvent(AbstractEvent ae) {
		ae.setGroup(this);
		this.events.add(ae);
	}
	
	/**
	 * Removes the event.
	 * <br>
	 * Automatically removes bidirectional connection between this Group and given AbstractEvent.
	 * 
	 * @param ae the ae
	 */
	public void removeEvent(AbstractEvent ae) {
		ae.setGroup(null);
		this.events.remove(ae);
	}
	
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
	// TODO
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Group)) return false;

        final Group group = (Group) o;

        if (!name.equals(group.name)) return false;
        if (!description.equals(group.description)) return false;
        if (discipline != null && group.discipline != null) {
        	if (!discipline.equals(group.discipline)) return false;
        }

        return true;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    // TODO
    public int hashCode() {
    	int result = 17;
    	result = 37 * result + getName().hashCode();
    	result = 37 * result + getDescription().hashCode();
    	if (getDiscipline() != null) {
    		result = 37 * result + getDiscipline().hashCode();
    	}
    	
    	return result;
    }
}
